package cn.slipi.common.annotation;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class UsedTimeAspect {
    public UsedTimeAspect() {
    }

    @Pointcut("@annotation(cn.slipi.common.annotation.UsedTime)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object aroundHandler(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        long startTime = System.currentTimeMillis();
        Object obj = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        log.info("当前时间:" + DateUtil.date() + ", 方法:" + methodName + "(),当前消耗时间:" + (endTime - startTime) + "ms," + (endTime - startTime) / 1000L + "s");
        return obj;
    }
}
